<?php

namespace app\api\controller;

use app\BaseController;
use app\api\model\View as ViewModel;
use app\api\model\Article as ArticleModel;
use app\api\model\Video as VideoModel;
use app\api\model\Voice as VoiceModel;

class View extends BaseController
{
    public function initialize()
    {
        $this->uid = $this->request->uid;
    }

    /* 历史记录 */
    public function list()
    {
        if (!$this->request->auth) return show(config('status.auth'), '认证失败');
        $page = input('page', 1, 'intval');
        $perpage = 12;
        $result = ViewModel::where('uid', $this->uid)->order('time desc')->page($page, $perpage)->select()->toArray();
        if ($result) {
            $data = [];
            foreach ($result as $v) {
                if ($v['type'] == 1) {
                    $one = VideoModel::where('id', $v['rid'])->field('title, profile')->find()->toArray();
                    $v['cover'] = $one['profile'];
                } elseif ($v['type'] == 2) {
                    $one =  VoiceModel::where('id', $v['rid'])->field('title, profile')->find()->toArray();
                    $v['cover'] = $one['profile'];
                } elseif ($v['type'] == 3) {
                    $one = ArticleModel::where('id', $v['rid'])->field('title, cover')->find()->toArray();
                    $v['cover'] = $one['cover'];
                }
                $v['title'] = $one['title'];
                $data[] = $v;
            }
            return show(200, '历史记录', $data);
        } else {
            return show(config('status.get_info_error'), '没有更多数据');
        }
    }

    /* 删除 */
    public function delete()
    {
        if (!$this->request->auth) return show(config('status.auth'), '认证失败');
        $id = input('id', 0, 'intval');
        $one = ViewModel::where('id', $id)->find();
        if (isset($one) && $one['uid'] == $this->uid) {
            $data = ViewModel::where($id)->delete();
            if ($data) {
                return show(200, '删除历史记录');
            } else {
                return show(config('status.error_delete'), '删除失败');
            }
        } else {
            return show(config('status.error_delete'), '删除失败');
        }
    }

    /* 批量删除 */
    public function deleteAll()
    {
        if (!$this->request->auth) return show(config('status.auth'), '认证失败');
        $ids = input('ids', '');
        $arr = explode(',', $ids);
        foreach ($arr as $v) {
            $uid = ViewModel::where('id', $v)->value('uid');
            if ($uid != $this->uid) {
                return show(config('status.error_delete'), '删除失败');
            }
        }
        $data = ViewModel::destroy($arr);
        if ($data) {
            return show(200, '删除历史记录');
        } else {
            return show(config('status.error_delete'), '删除失败');
        }
    }

    /* 清空 */
    public function flush()
    {
        if (!$this->request->auth) return show(config('status.auth'), '认证失败');
        $data = ViewModel::where('uid', $this->uid)->delete();
        if ($data) {
            return show(200, '清空历史记录');
        } else {
            return show(config('status.error_delete'), '清空失败');
        }
    }
}
